package com.myshoutui;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;

import com.myshout.Client;
import com.myshout.rest.RestClient;
import com.myshout.rest.RestResult;
import com.myshoutui.manager.MyShoutConsumerAsynTaskInterface;
import com.myshoutui.manager.MyShoutConsumerAsynTaskManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyShoutConsumerSignupActivity extends Activity implements
		MyShoutConsumerAsynTaskInterface, OnClickListener {

	Button mCreateBtn;
	Button mMaleBtn;
	Button mFemaleBtn;
	Button mNoGenderBtn;
	Button mAgreedChecked;
	EditText mFirstName;
	EditText mLastName;
	EditText mMobileNumber;
	EditText mEmailAddress;
	EditText mPassword;
	Boolean mChecked = true;
	private ProgressDialog progressDialog;
	MyShoutConsumerAsynTaskInterface mAsynListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.signup);
		this.mAsynListener = this;
		intializeViewComponents();

	}

	private void intializeViewComponents() {
		mCreateBtn = (Button) findViewById(R.id.signupcreateaccountbtn);
		mMaleBtn = (Button) findViewById(R.id.signupmale);
		mFemaleBtn = (Button) findViewById(R.id.signupfemale);
		mNoGenderBtn = (Button) findViewById(R.id.signnogender);
		mAgreedChecked = (Button) findViewById(R.id.agreecheck);

		mFirstName = (EditText) findViewById(R.id.signupfirstname);
		mLastName = (EditText) findViewById(R.id.signuplastname);
		mMobileNumber = (EditText) findViewById(R.id.signupmobilenumber);
		mEmailAddress = (EditText) findViewById(R.id.signupemailname);
		mPassword = (EditText) findViewById(R.id.signuppassword);
		TelephonyManager tMgr = (TelephonyManager) getApplicationContext()
				.getSystemService(Context.TELEPHONY_SERVICE);
		mMobileNumber.setText(tMgr.getLine1Number());
		mCreateBtn.setOnClickListener(this);
		mMaleBtn.setOnClickListener(this);
		mFemaleBtn.setOnClickListener(this);
		mNoGenderBtn.setOnClickListener(this);
		mAgreedChecked.setOnClickListener(this);
		mCreateBtn.setTypeface(Typeface.createFromAsset(getAssets(),
				"fonts/fontscore.com_h_helveticacondensed-bold.ttf"));

	}

	public Boolean doSignUp(Hashtable<String, String> task) {

		

		try {
			RestClient.post(Client.personRegisterResource, task);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	@Override
	public void startTask() {
		progressDialog = ProgressDialog.show(this, "",
				getString(R.string.please_wait_), true);
	}

	@Override
	public void endTask(Object result) {
		progressDialog.dismiss();
		if ((Boolean) result) {
			startActivity(new Intent(getApplicationContext(),
					MyShoutDashboardActivity.class));
			finish();
		} else {
			Toast.makeText(getApplicationContext(),
					"Account Creation Failed!!!", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public Object executeTask(Object... task) {
		// TODO Auto-generated method stub
		return doSignUp((Hashtable<String, String>)task[0]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.signupmale) {
			if (arg0.getResources().getDrawable(R.drawable.radiobtn) != null) {
				arg0.setBackgroundResource(R.drawable.raidobtnselected);
				mFemaleBtn.setBackgroundResource(R.drawable.radiobtn);
				mNoGenderBtn.setBackgroundResource(R.drawable.radiobtn);
			} else {
				arg0.setBackgroundResource(R.drawable.radiobtn);
			}
		} else if (arg0.getId() == R.id.signupfemale) {
			if (arg0.getResources().getDrawable(R.drawable.radiobtn) != null) {
				arg0.setBackgroundResource(R.drawable.raidobtnselected);
				mMaleBtn.setBackgroundResource(R.drawable.radiobtn);
				mNoGenderBtn.setBackgroundResource(R.drawable.radiobtn);
			} else {
				arg0.setBackgroundResource(R.drawable.radiobtn);
			}
		} else if (arg0.getId() == R.id.signnogender) {
			if (arg0.getResources().getDrawable(R.drawable.radiobtn) != null) {
				arg0.setBackgroundResource(R.drawable.raidobtnselected);
				mFemaleBtn.setBackgroundResource(R.drawable.radiobtn);
				mMaleBtn.setBackgroundResource(R.drawable.radiobtn);
			} else {
				arg0.setBackgroundResource(R.drawable.radiobtn);
			}
		} else if (arg0.getId() == R.id.agreecheck) {

			if (mChecked) {
				arg0.setBackgroundResource(R.drawable.checkboxchecked);
				mChecked = false;
			} else {
				arg0.setBackgroundResource(R.drawable.checkboxunchecked);
				mChecked = true;
			}

		} else if (arg0.getId() == R.id.signupcreateaccountbtn) {
			if (mFirstName.getText().toString().trim().length() == 0
					|| mLastName.getText().toString().trim().length() == 0
					|| mPassword.getText().toString().trim().length() == 0
					|| mEmailAddress.getText().toString().trim().length() == 0
					|| mMobileNumber.getText().toString().trim().length() == 0) {
				Toast.makeText(getApplicationContext(),
						"Please enter all the fields", Toast.LENGTH_LONG).show();
				return;
			} else if (mChecked) {
				Toast.makeText(getApplicationContext(),
						"Please accept the terms and condition ",
						Toast.LENGTH_LONG).show();
				return;
			}

			Hashtable<String, String> nvPairs = new Hashtable<String, String>();
			nvPairs.put("username", mFirstName.getText().toString().trim());
			nvPairs.put("password", mPassword.getText().toString().trim());
			nvPairs.put("firstname", mFirstName.getText().toString().trim());
			nvPairs.put("lastname", mLastName.getText().toString().trim());
			nvPairs.put("email", mEmailAddress.getText().toString().trim());
			nvPairs.put("store", "0");
			nvPairs.put("imageurl", "");
			nvPairs.put("usertype", "0");
			nvPairs.put("locale", Locale.getDefault().toString());
			nvPairs.put("phone", mMobileNumber.getText().toString().trim());
			nvPairs.put("phoneType", "1");

			new MyShoutConsumerAsynTaskManager(mAsynListener).execute(nvPairs);

		}
	}
}
